package com.infore.maintenance.main.dao;

import com.infore.common.model.entity.TBaseQuerySql;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数据增删改查 Mapper 接口
 * </p>
 *
 * @author lijh
 * @since 2022-12-01
 */
@Mapper
@Component
public interface TBaseQuerySqlMapper extends BaseMapper<TBaseQuerySql> {

    @Select(" ${sql} ")
    List<Map<String,Object>> queryBySql(@Param("sql")  String sql);


    @Select("${sql}")
    List<Map<String,Object>> queryBySqlAndParameters(@Param("sql")  String sql,@Param("map") Map<String, Object> map );


    @Select("select * from data_maintain.t_base_query_sql where sql_code=#{sqlCode}")
    TBaseQuerySql queryBySqlCode(@Param("sqlCode")  String sqlCode);

    @Insert(" ${sql} ")
    int insertBySql(@Param("sql")  String sql);

    @Insert("<script> " +
            "insert into" +
            "        ${map.tableName}" +
            "        (\n" +
            "        <foreach collection=\"map.mapColumnProperty\" index=\"key\" item=\"value\" separator=\",\">" +
            "        `${key}`" +
            "        </foreach>" +
            "        )\n" +
            "        values\n" +
            "        <foreach collection=\"map.data\" item=\"line\" separator=\",\">" +
            "            (\n" +
            "            <foreach collection=\"line\" index=\"key\" item=\"value\" separator=\",\">" +
            "                #{value}" +
            "            </foreach>" +
            "            )\n" +
            "        </foreach>" +
            " </script>")
    int insertTableListDynamicOfMysql(@Param(value = "map") Map<String, Object> map);



    @Insert("<script> " +
            "insert into" +
            "        ${map.tableName}" +
            "        (\n" +
            "        <foreach collection=\"map.mapColumnProperty\" index=\"key\" item=\"value\" separator=\",\">" +
            "        `${key}`" +
            "        </foreach>" +
            "        )\n" +
            "        <foreach collection=\"map.data\" item=\"line\" separator=\"UNION ALL\">" +
            "            SELECT " +
            "            <foreach collection=\"line\" index=\"key\" item=\"value\" separator=\",\">" +
            "                #{value}" +
            "            </foreach>" +
            "            FROM dual " +
            "        </foreach>" +
            " </script>")
    int insertTableListDynamicOfOracle(@Param(value = "map") Map<String, Object> map);


    @Insert("<script> " +
            "insert into" +
            "        ${map.tableName}" +
            "        (\n" +
            "        <foreach collection=\"map.mapColumnProperty\" index=\"key\" item=\"value\" separator=\",\">" +
            "        `${key}`" +
            "        </foreach>" +
            "        )\n" +
            "        values\n" +
            "       (\n" +
            "       <foreach collection=\"map.data\" index=\"key\" item=\"value\" separator=\",\">" +
            "           #{value}" +
            "       </foreach>" +
            "       )\n" +
            " </script>")
    int insertTableEntityDynamic(@Param(value = "map") Map<String, Object> map);

    @Update(" ${sql} ")
    int updateBySql(@Param("sql")  String sql);


    @Update(" ${sql} ")
    int updateBySqlAndMap(@Param("sql")  String sql,@Param("map") Map<String, Object> map);

    @Update("<script> " +
            "update" +
            "        ${map.tableName}  " +
            "        set\n" +
            "        <foreach collection=\"map.setColumnAndValue\" index=\"key\" item=\"value\" separator=\",\">" +
            "        `${key}`= #{value} " +
            "        </foreach>" +
            "        where  \n" +
            "       <foreach collection=\"map.whereColumnAndValue\" index=\"key\" item=\"value\" separator=\"and\">" +
            "        `${key}`= #{value}" +
            "       </foreach>" +
            " </script>")
    int updateTableEntityDynamic(@Param(value = "map") Map<String, Object> map);


    @Delete(" ${sql} ")
    int deleteBySql(@Param("sql")  String sql);


    @Update(" ${sql} ")
    int deleteBySqlAndMap(@Param("sql")  String sql,@Param("map") Map<String, Object> map);

    @Delete("<script> " +
            "delete from" +
            "        ${map.tableName}  " +
            "        where  \n" +
            "       <foreach collection=\"map.whereColumnAndValue\" index=\"key\" item=\"value\" separator=\"and\">" +
            "        `${key}`= #{value}" +
            "       </foreach>" +
            " </script>")
    int deleteTableEntityDynamic(@Param(value = "map") Map<String, Object> map);


}
